package com.example.match.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.match.model.domain.User;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.redisson.api.RList;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author 雨后天晴
 * @version 1.0
 * @since 2024/10/7 17:18
 */
@Slf4j
@SpringBootTest
public class RedissonTest {

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    // 重点用户
    private final List<Long> mainUserList = Collections.singletonList(1L);

    @Test
    void test() {
        // list,数据存在本地JVM内存中
        ArrayList<String> list = new ArrayList<>();
        list.add("sunny");
        String s1 = list.get(0);
        System.out.println(s1);
        list.remove(0);

        // 数据存在redis内存中
        RList<String> rList = redissonClient.getList("test-list");
        // rList.add("sunny");
        String s2 = rList.get(0);
        System.out.println(s2);
        rList.remove(0);
        // map
        Map<String, Integer> map = new HashMap<>();
        map.put("sunny", 18);
        Integer age = map.get("sunny");
        System.out.println(age);

        // set

        // stack
    }

    @Test
    void test2() {
        RLock lock = redissonClient.getLock("match:preCacheJob:doCache:lock");
        try {
            // 只有一个线程能获取到锁
            if (lock.tryLock(0, -1, TimeUnit.MILLISECONDS)) {
                Thread.sleep(300000);
                System.out.println("getLock: " + Thread.currentThread().getId());
                for (Long userId : mainUserList) {
                    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                    Page<User> userPage = userService.page(new Page<>(1, 20), queryWrapper);
                    String redisKey = String.format("match:user:recommend:%s", userId);
                    ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
                    // 写缓存
                    try {
                        valueOperations.set(redisKey, userPage, 30000, TimeUnit.MILLISECONDS);
                    } catch (Exception e) {
                        log.error("Redis set key error", e);
                    }
                }
            }
        } catch (InterruptedException e) {
            log.error("doCacheRecommendUser error", e);
        } finally {
            // 只能释放自己的锁
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }
}
